package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//菜品管理
@RestController
public class DishController {

    @Autowired
    private DishService dishService;


    //分页条件查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,//设置起始页
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,//设置每页个数
            String name) {
        //调用service层方法查询
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        //将封装的参数传入回显
        return ResultInfo.success(page);

    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {//接受参数
        //调用service层方法
        dishService.save(dish);//将参数传入
        //返回结果
        return ResultInfo.success();
    }

    //修改：菜品回显
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        //调用service层的查询方法
        Dish dish = dishService.findById(id);//将参数传入
        //返回结果回显
        return ResultInfo.success(dish);
    }

    //菜品修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        //调用service层的修改方法
        dishService.update(dish);//将参数传入
        //返回结果
        return ResultInfo.success();
    }

    //起售停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStastus(@PathVariable("status") Integer status,
                                    @RequestParam List<Long> ids) {//list集合需要注解
        //调用service层方法
        dishService.updateStatus(status, ids);//将接受的参数传入
        //返回结果
        return ResultInfo.success();
    }

    //菜品删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) {
        //调用service层的查询方法
        dishService.deleteBatchIds(ids);//将参数传入
        //返回结果
        return ResultInfo.success();
    }
    //套餐新增回显
    @GetMapping("dish/list")
    public ResultInfo findByType(Long categoryId,String name){
        //前端传入的参数调用
        List<Dish> setmealList = dishService.findByType(categoryId,name);
        //
        return ResultInfo.success(setmealList);
    }

}
